package com.yiyoucheng.controller;

import com.yiyoucheng.entity.PageResult;
import com.yiyoucheng.entity.QueryCondition;
import com.yiyoucheng.entity.QueryPageBean;
import com.yiyoucheng.entity.Result;
import com.yiyoucheng.pojo.Employee;
import com.yiyoucheng.service.DepartmentService;
import com.yiyoucheng.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工信息管理
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private DepartmentService departmentService;

    @GetMapping("/findAll")
    public List<Employee> findAll(){
        return employeeService.findAll();
    }

    /*//员工信息分页查询带条件
    @GetMapping(value = "/findPage/{currentPage}/{pageSize}")
    public PageResult findPage(@RequestParam String queryString,@PathVariable int currentPage,@PathVariable int pageSize){
        PageResult page = employeeService.findPage(queryString, currentPage, pageSize);
        return page;
    }*/

    //员工信息条件+分页查询
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        //获取查询条件
        QueryCondition queryCondition = queryPageBean.getQueryCondition();
        //List<Integer[]> queryDepIdArray = queryCondition.getQueryDepIdArrayL();
        //获取前端传来的多选部门层级数组
        Integer[][] queryDepIdArrays = queryCondition.getQueryDepIdArrays();
        if(queryDepIdArrays != null && queryDepIdArrays.length >0){
            //作为查询条件的部门id数组
            Integer[] queryDepIds = new Integer[queryDepIdArrays.length];
            //把多选的部门层架数组里的每一个数组的最后一个元素取出来,并将其存到部门id数组里去
            for (int i = 0; i < queryDepIdArrays.length; i++) {
                Integer[] queryDepIdArray = queryDepIdArrays[i];
                //获取最后一个数组元素(这个值为所需的部门id)
                Integer depId = queryDepIdArray[queryDepIdArray.length - 1];
                //把这个id存到部门id数组里去
                queryDepIds[i] = depId;
            }
            //把需要查询的部门id数组存回查询条件对象去
            queryCondition.setQueryDepIds(queryDepIds);
        }
        /*Integer depId = null;
        if (queryDepIdArray != null && queryDepIdArray.length >0){
             *//*depId = queryDepIdArray[queryDepIdArray.length-1];
            queryCondition.setQueryDepId(depId);*//*
             queryDepIdArray.for
        }*/
        queryPageBean.setQueryCondition(queryCondition);
        PageResult pageResult = employeeService.findPage(queryPageBean);
        return pageResult;
    }

    //根据id查询员工信息
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id){
        try {
            Employee employee = employeeService.findById(id);
            //将员工所在的部门层级数组查询出来,并将相关信息添加到Map中存储,将map返回
            Map map = employeeToMap(employee);
            return new Result(true,"查询员工信息成功",map);
        } catch (Exception e) {
            return new Result(false,"查询员工信息失败");
        }
    }

    //将员工信息补充完整并存到map中
    public Map employeeToMap(Employee employee){
        //查询部门的层级关系 查询结果为 1/2/3 这样的形式
        String level = departmentService.findLevel(employee.getDepId());
        //将字符串按照 "/" 进行分割,得到部门id的字符串数组
        String[] levelArray = level.split("/");
        //将字符串数组转换为整形数组
        Integer[] depId = new Integer[levelArray.length];
        for(int i = 0;i<depId.length;i++){
            depId[i] = Integer.parseInt(levelArray[i]);
        }
        Map map = new HashMap();
        map.put("id",employee.getId());
        map.put("code",employee.getCode());
        map.put("name",employee.getName());
        map.put("telephone",employee.getTelephone());
        map.put("address",employee.getAddress());
        map.put("depId",depId);
        return map;
    }

    //新增员工信息
    @PostMapping("/add")
    public Result add(@RequestBody Map map){
        //将map里的数据添加到Employee对象中
        Employee employee = mapToEmployee(map);
        try {
            employeeService.add(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"新增员工信息失败");
        }
        return new Result(true,"新增员工信息成功");
    }

    //把前端页面传过来的数据加到Employee类中
    public Employee mapToEmployee(Map map){
        Employee employee = new Employee();
        if(map.get("id") != null){
            employee.setId(Integer.parseInt(map.get("id").toString()));
        }
        employee.setCode((String) map.get("code"));
        employee.setName((String) map.get("name"));
        employee.setTelephone((String) map.get("telephone"));
        employee.setAddress((String) map.get("address"));
        ArrayList depIdList = (ArrayList) map.get("depId");
        if(depIdList.size() >0 && depIdList != null){
            //集合中最后一个值为员工所在部门的id
            employee.setDepId((Integer) depIdList.get(depIdList.size()-1));
        }
        return employee;
    }

    //更改员工信息
    @PutMapping("/update")
    public Result update(@RequestBody Map map){
        Employee employee = mapToEmployee(map);
        try {
            employeeService.update(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"更改员工信息失败");
        }
        return new Result(true,"更改员工信息成功");
    }

    //根据id删除员工信息(软删除)
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam Integer id){
        try {
            employeeService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除员工信息失败");
        }
        return new Result(true,"删除员工信息成功");
    }

    //根据员工编号查询员工信息
    @GetMapping("/findByCode")
    public Result findByCode(@RequestParam String code){
        Employee employee = employeeService.findByCode(code);
        if(employee != null){
            return new Result(true,"该员工编号已被使用,请重新填写"/*,employee*/);
        }else{
            return new Result(false,"该员工编号不存在");
        }
    }

    //查询所有员工地址
    @GetMapping("/findAddressList")
    public List<Map> findAddressList(){
        return employeeService.findAddressList();
    }

    //查询所有员工的姓名
    @GetMapping("/findNameList")
    public List<Map> findNameList(){
        return employeeService.findNameList();
    }

}
